<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of index
 *
 * @author Administrator
 */
class index extends CI_Controller {

  public function __construct() {
    parent::__construct();
    set_userdata('end_user_current_url', current_url());
    if (!userdata('country_id') || !userdata('state_id') || !userdata('city_id')) {
      if (!userdata('end_user_user_id')) {
        redirect('welcome');
      } else {
        redirect('welcome/location');
      }
      return;
    }
  }
  
  public function location(){
    if(isset($_POST['country_id']))
      set_userdata('country_id', $_POST['country_id']);
    if(isset($_POST['state_id']))
      set_userdata('state_id', $_POST['state_id']);
	if(isset($_POST['city_id']))
      set_userdata('city_id', $_POST['city_id']);
	if(!empty($_POST['current_url']))
	  redirect($_POST['current_url']);
	else redirect('');
  }

  public function index($max = '', $offset = '') {
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $conditions = array('status' => PROMOTION_PUBLISH, 'start_date <= ' => time(), 'end_date >= ' => time(), 'remain > ' => 0);
    if (userdata('country_id'))
      $conditions['country_id'] = userdata('country_id');
    if (userdata('state_id'))
      $conditions['state_id'] = userdata('state_id');
    if (userdata('city_id'))
      $conditions['city_id'] = userdata('city_id');
    $promotions = $this->promotion_model->get_all($conditions, null, null, null, null, 
            null, null, null, null, null, null, null, array('end_date' => 'asc'), $max, $offset);
    foreach ($promotions as $i => $promotion) {
      $promotions[$i]['medias'] = $this->promotion_media_model->get_all(array('promotion_id' => $promotion['id']));
      $promotions[$i]['num_got'] = $this->promotion_download_model->count(array('promotion_id' => $promotion['id']));
    }
    $total = $this->promotion_model->count($conditions);
    $this->load->helper('paging');
    $paging = paging('index/index', $total, $max, $offset);
    $this->load->view('user/index', array(
        'promotions' => $promotions, 'paging' => $paging,
        'title_html' => $this->lang->line('front-end.title.index'),
    ));
  }

  public function category($id = '', $nice_name = '', $max = '', $offset = '') {
    $category = $this->category_model->get($id);
    if ($category) {
      $offset = $offset + 0;
      $max = $this->setting_model->item('max_record');
      $cat_pro = $this->category_promotion_model->get_all(array('cat_id' => $id));
      $temp = array('id' => array(0));
      if (!empty($cat_pro)) {
        foreach ($cat_pro as $pro) {
          $temp['id'][] = $pro['promotion_id'];
        }
      }
      $conditions = array('status' => PROMOTION_PUBLISH, 'start_date <= ' => time(), 'end_date >= ' => time(), 'remain > ' => 0);
      if (userdata('country_id'))
        $conditions['country_id'] = userdata('country_id');
      if (userdata('state_id'))
        $conditions['state_id'] = userdata('state_id');
      if (userdata('city_id'))
        $conditions['city_id'] = userdata('city_id');
      $promotions = $this->promotion_model->get_all($conditions, null, $temp, null, null, null, null, null, null, null, null, null, array('end_date' => 'asc'), $max, $offset);
      foreach ($promotions as $i => $promotion) {
        $promotions[$i]['medias'] = $this->promotion_media_model->get_all(array('promotion_id' => $promotion['id']));
        $promotions[$i]['num_got'] = $this->promotion_download_model->count(array('promotion_id' => $promotion['id']));
      }
      $total = $this->promotion_model->count($conditions, null, $temp);
      $this->load->helper('paging');
      $paging = paging('index/category', $total, $max, $offset, array($id, $nice_name));
      $this->load->view('user/index', array(
          'promotions' => $promotions, 'paging' => $paging,
          'title_html' => $this->lang->line('front-end.title.index'),
          'parent_id' => empty($category['parent_id']) ? $id : $category['parent_id'],
          'category_id' => empty($category['parent_id']) ? 0 : $category['id'],
              )
      );
    } else {
      redirect('');
    }
  }

  public function provider($provider_id = '', $max = '', $offset = '') {
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $conditions = array('status' => PROMOTION_PUBLISH,'is_active' => TRUE, 'start_date <= ' => time(),
        'end_date >= ' => time(), 'remain > ' => 0, 'provider_user_id' => $provider_id);    
    $promotions = $this->promotion_model->get_all($conditions, null, null, null, 
            null, null, null, null, null, null, null, null, array('end_date' => 'asc'), $max, $offset);
    foreach ($promotions as $i => $promotion) {
      $promotions[$i]['medias'] = $this->promotion_media_model->get_all(array('promotion_id' => $promotion['id']));
      $promotions[$i]['num_got'] = $this->promotion_download_model->count(array('promotion_id' => $promotion['id']));
    }
    $total = $this->promotion_model->count($conditions);
    $this->load->helper('paging');
    $paging = paging('index/provider', $total, $max, $offset, $provider_id);
    $this->load->view('user/index', array(
        'promotions' => $promotions, 'paging' => $paging,
        'title_html' => $this->lang->line('front-end.title.index'),
    ));
  }
}